package com.satis.compose.get.widget

/**
 * @author sunshaobei on 2022/9/8
 */
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.ConstraintSet
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.satis.compose.get.statusBarHeightDp

/**
 * @param title 标题
 * @param modifier Modifier，设置后会和已经设置好Modifier进行合并
 * @param navigationIcon 左边图标
 * @param backgroundColor 背景颜色
 * @param actions 右边图标
 */
@Composable
fun GetTopAppBar(
    title: @Composable () -> Unit,
    appbarHeight:Dp = 56.dp,
    ignoreStatusBar:Boolean = false,
    navigationIcon: @Composable (() -> Unit)? = null,
    backgroundColor: Color = MaterialTheme.colors.primarySurface,
    actions: @Composable RowScope.() -> Unit = {},
) {
    val statusBarHeight = if (ignoreStatusBar) 0.dp else statusBarHeightDp
    val constraintSet = ConstraintSet {
        val titleRef = createRefFor("title")
        val navigationIconRef = createRefFor("navigationIcon")
        val actionsRef = createRefFor("actions")
        constrain(titleRef) {
            start.linkTo(parent.start)
            end.linkTo(parent.end)
            top.linkTo(parent.top)
            bottom.linkTo(parent.bottom)
        }
        constrain(navigationIconRef) {
            top.linkTo(parent.top)
            bottom.linkTo(parent.bottom)
        }
        constrain(actionsRef) {
            top.linkTo(parent.top)
            bottom.linkTo(parent.bottom)
            end.linkTo(parent.end)
        }
    }
    ConstraintLayout(
        constraintSet, modifier = Modifier
            .fillMaxWidth()
            .background(backgroundColor)
            .height(appbarHeight + statusBarHeight)
            .padding(top = statusBarHeight)
    ) {
        Box(
            Modifier
                .layoutId("title")
                .padding(horizontal = 4.dp)
        ) {
            ProvideTextStyle(value = MaterialTheme.typography.h6) {
                CompositionLocalProvider(
                    LocalContentAlpha provides ContentAlpha.high,
                    content = title
                )
            }
        }
        if (navigationIcon != null) {
            Box(
                modifier = Modifier
                    .layoutId("navigationIcon")
                    .padding(start = 4.dp)
            ) {
                CompositionLocalProvider(
                    LocalContentAlpha provides ContentAlpha.high,
                    content = navigationIcon
                )
            }
        }
        Row(
            Modifier
                .layoutId("actions")
                .padding(end = 4.dp),
            content = actions
        )
    }
}